#include "Registro.h"

Registro::Registro()
{
        tamanioBuffer = 0;
        buffer = NULL;
        offsetBuffer = 0;
};

Registro::~Registro()
{
        if (buffer != NULL)
        {
                delete[] buffer;
        }
};

char* Registro::getBuffer() const
{
        return buffer;
};

unsigned short Registro::getTamanioBuffer() const
{
        return tamanioBuffer;
};

void Registro::setTamanioBuffer(unsigned short tamanioBuffer)
{
        this->tamanioBuffer = tamanioBuffer;
};

bool Registro::setBuffer(char* buffer)
{
        if (crearBuffer())
        {
                try
                {
                        memcpy(this->buffer, buffer, tamanioBuffer);
                        offsetBuffer = 0;
                        return true;
                }
                catch(...)
                {
                        return false;
                }
        }

        return false;
};

bool Registro::crearBuffer()
{
        delete[] buffer;
        buffer = new char[tamanioBuffer];
        if (buffer != NULL)
        {
                memset(buffer, '\0', tamanioBuffer);
                offsetBuffer = 0;
                return true;
        }
        
        return false;
};

bool Registro::limpiarBuffer()
{
        if (buffer!= NULL)
        {
                memset(buffer, '\0', tamanioBuffer);
                offsetBuffer = 0;
                return true;
        }

        return false;
};


void Registro::putIntoBuffer(void* source, unsigned short size)
{
        if (this->buffer != NULL)
        {
                memcpy(this->buffer + this->offsetBuffer, source, size);
                offsetBuffer += size;
        }
}

void Registro::getFromBuffer(void* target, unsigned short size)
{
        if (this->buffer != NULL)
        {
                memcpy(target, this->getBuffer() + this->offsetBuffer, size);
                offsetBuffer += size;
        }
}

